<?php
// +----------------------------------------------------------------------
// | Author: huangzhen_20210406
// +----------------------------------------------------------------------

namespace app\common\model;

use think\facade\Db;

class UserSetLog extends Common
{
    /**
     * 用过user访问user child
     * @param $post
     * @param $user_id
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function tableDataAdmin($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where =[];
        if(isset($post['username']) && !empty($post['username'])){
            $where[] = ['u.username', 'like', '%' . trim($post['username']) . '%'];
        }
        if(isset($post['datetime']) && !empty($post['datetime'])){
            $time_list = explode('_' , $post['datetime']);
            $start_time = strtotime($time_list[0] . " 00:00:00");
            $end_time   = strtotime($time_list[1] . " 23:59:59");
            $where[] = ['uf.ctime', 'between', "{$start_time},{$end_time}"];
        }
        $list = Db::name('user_set_log')->alias('uf')
                ->leftJoin("user u", "u.id=uf.user_id")
                ->leftJoin("manage m" , "m.id=uf.manage_id")
                ->field('uf.* , u.username , m.username un')
                ->where($where)
                ->order('uf.ctime desc')
                ->paginate(['list_rows' => $list_rows, 'page' => $page])
                ->toArray();

        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }

}